package com.lagou.edu.service.impl;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.ResumeDO;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ResumeSeviceImpl implements ResumeService {
    @Autowired
    ResumeDao resumeDao;

    @Override
    public ResumeDO findOne(long id) {
        return resumeDao.getOne(id);
    }

    @Override
    public List<ResumeDO> findAll(String key) {
        if (null == key || "".equals(key))
            return resumeDao.findAll();
        return resumeDao.findByNameLike(key);
    }

    @Override
    public void save(List resumes) {
        for (int i = 0, l = resumes.size(); i < l; i++) {
            HashMap o = (HashMap) resumes.get(i);

            String id = o.get("id") != null ? o.get("id").toString() : null;
            String state = o.get("_state") != null ? o.get("_state").toString() : "";
            String address = o.get("address") != null ? o.get("address").toString() : "";
            String phone = o.get("phone") != null ? o.get("phone").toString() : "";
            String name = o.get("name") != null ? o.get("name").toString() : "";
            ResumeDO resumeDO = new ResumeDO();
            resumeDO.setAddress(address);
            Long idl = null == id ? null : Long.parseLong(id);
            resumeDO.setId(idl);
            resumeDO.setName(name);
            resumeDO.setPhone(phone);
            if (state.equals("added") || null == id)                //新增：id为空，或_state为added
            {
//                o.put("createtime", new Date());
                resumeDao.save(resumeDO);
            } else if (state.equals("removed") || state.equals("deleted")) {
                resumeDao.delete(resumeDO);
            } else if (state.equals("modified") || state.equals(""))    //更新：_state为空，或modified
            {
                resumeDao.save(resumeDO);
            }
        }
//        resumes.forEach((resume) -> resumeDao.save(resume));
    }

    @Override
    public void update(List<ResumeDO> resumes) {
        resumes.forEach(resume -> resumeDao.save(resume));
    }

    @Override
    public void delete(long id) {
        resumeDao.deleteById(id);
    }
}
